package com.diver.flinkdemo.service.impl;

import com.diver.flinkdemo.entity.Trd.ProvincialZhongTai.StudentLiveness;
import com.diver.flinkdemo.entity.xxt.StudentCard;
import com.diver.flinkdemo.mapper.mysqlMapper.StudentLivenessMapper;
import com.diver.flinkdemo.service.IStudentLivenessService;
import com.diver.flinkdemo.utils.ConstantString;
import com.diver.flinkdemo.utils.DateUtil;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
/**
 * @author LuJW
 * @date 2023/4/4 13:15
 */
@Slf4j
public class StudentLivenessServiceImpl implements IStudentLivenessService {

    private StudentLivenessMapper mapper;
    private SqlSession session;
    @Override
    public int saveStudentLiveness(StudentCard studentCard, String opcode, String opname, String oplasttime) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(StudentLivenessMapper.class);
            log.info("studentCard->{}",studentCard.toString());
            if(studentCard.getIsPushSZT()){//是否推送。
                StudentLiveness studentLiveness = new StudentLiveness();
                studentLiveness.setCompanyid(ConstantString.COMPANY_ID);
                studentLiveness.setCompanyname(ConstantString.COMPANY_NAME);
                String nowDateString = DateUtil.getNowDateString();
                studentLiveness.setCalcdate(DateUtil.getNowDateWholePoint("yyyyMMdd"));
                studentLiveness.setSchoolname(studentCard.getSchoolName());
                studentLiveness.setRealname(studentCard.getName());
                studentLiveness.setImei(studentCard.getImei());
                studentLiveness.setIdcardno(studentCard.getIdCard());
                studentLiveness.setOpcode(opcode);
                studentLiveness.setOpname(opname);
                studentLiveness.setOptime(DateUtil.getNowDateWholePoint("yyyyMMddHHmmss"));
                if (oplasttime != null && !"".equals(oplasttime)) {
                    studentLiveness.setOplasttime(oplasttime);
                }
                studentLiveness.setIsPush(0);
                studentLiveness.setAddTime(nowDateString);
                return mapper.insert(studentLiveness);
            }
            return 0;
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
